//
//  TravelRouteCollectionViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/19.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "TravelRouteCollectionViewController.h"
#import "TravelRouteCollectionViewCell.h"
#import "TravelRouteCollectionReusableView.h"
#import "TravelltineraryModel.h"
#import "RouteListTableViewController.h"
@interface TravelRouteCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collection;
@property(nonatomic,strong)NSMutableArray *arr;
@end

@implementation TravelRouteCollectionViewController
-(NSMutableArray *)arr{
    if(!_arr){
        self.arr=[NSMutableArray array];
    }
    return _arr;
}

-(void)JSONData{
    [LORequestManger GET:kltineraryURL success:^(id response) {
        NSDictionary * dic=(NSDictionary *)response;
        for (NSDictionary *val in dic[@"regions"]) {
            TravelltineraryModel *model=[TravelltineraryModel new];
            [model setValuesForKeysWithDictionary:val];
            [self.arr addObject:model];
        }
        [self setcollection];
        [self.collection reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
        
    }];

}
-(void)setcollection{
    
    //创建一个布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置集合视图的行间距
    layout.minimumLineSpacing = 10;
    //获取屏幕的宽
    CGFloat w = kWidth;
    //设置集合视图中每个item的大小
    layout.itemSize = CGSizeMake((w-50)/3, 140);
    
    //设置集合视图增广视图的大小
    layout.headerReferenceSize = CGSizeMake(w, 30);
    
    //创建一个集合视图
    self.collection = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    //指定集合视图数据源
    self.collection.dataSource = self;
    self.collection.delegate = self;
    [self.view addSubview:self.collection];
    [self.collection registerClass:[TravelRouteCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collection registerClass:[TravelRouteCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classView"];
    self.collection.backgroundColor=[UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent=NO;
    
    [self JSONData];
    
}
//设置item间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10, 0, 10);

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.arr.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RouteListTableViewController *route=[RouteListTableViewController new];
     TravelltineraryModel *model=self.arr[indexPath.section];
    route.url=[NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v2/trails?&page_size=20&area_id=&page=1&search=%@&device_type=2&search_in=1&app_version=4.1.5&api_version=1&access_token=",[model.province[indexPath.row][@"name"]stringByAddingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)]];
    [self.navigationController pushViewController:route animated:YES];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    TravelltineraryModel *model=self.arr[section];
    return model.province.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TravelRouteCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    TravelltineraryModel *model=self.arr[indexPath.section];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:model.province[indexPath.row][@"icon"]]];
    cell.lab.text=[NSString stringWithFormat:@"路线%@",model.province[indexPath.row][@"trail_count"]];
    cell.backgroundColor=[UIColor colorWithRed:231%256/255.0 green:231%256/255.0 blue:231%256/255.0 alpha:1];
    return cell;
}
//设置头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    TravelRouteCollectionReusableView *reus=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"classView" forIndexPath:indexPath];
    TravelltineraryModel *model=self.arr[indexPath.section];
    reus.lable.text=model.region;
    return reus;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
